#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>


struct stu
{
	char name[20];
	int age;
};


int_struct_name_cmp(const void* e1, const void* e2)
{
	return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
}


int struct_age_cmp(const void* e1, const void* e2)
{
	return (((struct stu*)e1)->age - ((struct stu*)e2)->age);
}


int int_cmp(const void* e1, const void* e2)
{
	return (*(int*)e1 - *(int*)e2);
}



void Swap(char* B1, char* B2, int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		char tmp = *B1;
		*B1 = *B2;
		*B2 = tmp;
		B1++;
		B2++;

	}
}

void bubble_sort(void* base, int sz, int num, int (*cmp)(const void* e1, const void* e2))
{
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base +j*num,(char*)base + (j + 1) * num) > 0)
			{
				Swap((char*)base + j * num, (char*)base + (j + 1) * num,num);
			}
		}
	}
}

void print(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	
	printf("\n");
}

void test1()
{
	int arr[10] = { 2,6,1,5,3,4,9,8,7,10 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int num = sizeof(arr[0]);
	bubble_sort(arr, sz, num, int_cmp);
	print(arr,sz);
}


void test2()
{
	struct stu s[] = { {"zhangsan",21},{"lisi",42},{"wangwu",53} };
	int sz = sizeof(s) / sizeof(s[0]);
	int num = sizeof(s[0]);
	bubble_sort(s, sz, num, struct_age_cmp);

	
}



int main()
{
	
	//test1();
	test2();
	return 0;
}